#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
ThinkV - A Util for ThinkPads
This application handles everything useful for a Lenovo ThinkPad
'''
__author__ = 'Viktor Nagy'
__email__ = 'viktor.nagy@gmail.com'
__licence__ = '''
Copyright (c) 2008 Viktor Nagy

This file is part of ThinkV - A Util for ThinkPads.

ThinkV - A Util for ThinkPads is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ThinkV - A Util for ThinkPads is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ThinkV - A Util for ThinkPads.  If not, see <http://www.gnu.org/licenses/>.
'''

import sys, os.path
import optparse
import wx
import modules as m

ID_ABOUT = wx.NewId()
ID_EXIT = wx.NewId()

class ThinkV(wx.Frame):
    
    def __init__(self, args):
        # initialize the system
        self.loadModules()
        self.cfg = ConfigManager(self.modules)
        
        # prepare the layout
        wx.Frame.__init__(self, None, wx.ID_ANY, 'ThinkV', size=(300, 200))
        self.makeMenu()
        self.makePage()
        self.Show(True)
        
    def loadModules(self):
        '''
        Load all the modules
        '''
        self.modules = dict(zip(
                [module for module in dir(m) if callable(getattr(m, module))], 
                [getattr(m, module)(self) for module in dir(m) if callable(getattr(m, module))]))
        self.modules.pop('BaseModule')
        debug(self.modules, 'modules')
        
    def makeMenu(self):
        filemenu= wx.Menu()
        filemenu.Append(ID_ABOUT, "&About"," Information about this program")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
        wx.EVT_MENU(self, ID_EXIT, self.OnExit)
        
    def makePage(self):
        '''Generate a list of available options'''
        for module in self.modules.values():
            module.getController()
        
    def OnAbout(self, event):
        d= wx.MessageDialog( self, '''A simple script to handle all the beauty of a Lenovo T61
            Created by Viktor Nagy''',"About Sample Editor", wx.OK)
        d.ShowModal()
        d.Destroy()

    def OnExit(self, event):
        self.cfg.Flush()
        self.Close(True)
        
    def errorMessage(self, msg):
        print msg
        

def debug(msg, string=None):
    print string and '%s - %s' % (string, msg) or msg


class ConfigManager(wx.Config):
    
    prefs = {}
    defaults = {}
    
    def __init__(self, modules):
        self.parseDefaults(modules)
        getattr(super(ConfigManager, self), '__init__')(appName='ThinkV')
        
    def parseDefaults(self, modules):
        '''
        Get the defaults values from the modules
        '''
        for module in modules.values():
            self.defaults.update(module.defaultConfig)
    

if __name__=='__main__':
    app = wx.PySimpleApp()
    frame = ThinkV(sys.argv[1:])
    app.MainLoop()
